package com.stupro.cjlu.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.stupro.cjlu.common.vo.TeacherCourseItemVO;
import com.stupro.cjlu.common.vo.TimetableItemVO;
import com.stupro.cjlu.entity.Teacher;
import com.stupro.cjlu.mapper.TeacherMapper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class TeacherDAO extends BaseDAO{
    public static final int PAGE_SIZE = 4;

    private final TeacherMapper mapper;

    public TeacherDAO(TeacherMapper mapper) {
        this.mapper = mapper;
    }

    public int insert(Teacher entity) {
        return mapper.insert(entity);
    }

    public int delete(Integer id) {
        return mapper.deleteById(id);
    }

    public Teacher get(Integer teaID) {
        return mapper.selectById(teaID);
    }

    public int update(Teacher entity) {
        return mapper.updateById(entity);
    }

    public int count(String name) {
        return mapper.count(name);
    }

    public IPage<Teacher> getByPage(Integer index, String name) {
        Page<Teacher> page = new Page<>(index, PAGE_SIZE);
        LambdaQueryWrapper<Teacher> wrapper = new LambdaQueryWrapper();
        wrapper.like(StringUtils.isNotBlank(name),Teacher::getTeaName,name);
        return mapper.selectPage(page,wrapper);
    }

    public IPage<TeacherCourseItemVO> getCourserByPage(Integer index, String tName, String cName){
        Page<TeacherCourseItemVO> page = new Page<>(index, PAGE_SIZE);
        tName = tName==null?"":tName;
        cName = cName==null?"":cName;
        return mapper.selectCoursePage(page,tName,cName);
    }

    public Teacher getByNumber(String number) {
        LambdaQueryWrapper<Teacher> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Teacher::getTeaUserName, number);

        return mapper.selectOne(wrapper);
    }

    /*public Integer countByDepartmentId(Integer departmentId) {
        LambdaQueryWrapper<Teacher> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Teacher::getDepartmentId, departmentId);

        return mapper.selectCount(wrapper);
    }*/

    public List<Teacher> listName() {
        LambdaQueryWrapper<Teacher> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(Teacher::getTeaID, Teacher::getTeaName);

        return mapper.selectList(wrapper);
    }

    public List<TimetableItemVO> listTeacherTimetable(Integer teacherId) {
        return mapper.listTeacherTimetable(teacherId);
    }

//    public List<TeacherCourseItemVO> listTeacherCourse(Integer teacherId) {
//        return mapper.listTeacherCourse(teacherId);
//    }

    public int delById(int id) {
        return mapper.deleteById(id);
    }

    public List<TeacherCourseItemVO> getCourseByTeacherId(int teaID) {

        return mapper.getCourseByTeacherId(teaID);
    }


    public Boolean getByTeacher(Teacher teacher) {
        LambdaQueryWrapper<Teacher> wrapper = new LambdaQueryWrapper();

        wrapper.eq(Teacher::getTeaID,teacher.getTeaID())
                .eq(Teacher::getTeaName,teacher.getTeaName())
                .eq(Teacher::getTeaPwd,teacher.getTeaPwd());

        if(mapper.selectOne(wrapper)!=null){
            return true;
        }else{
            return false;
        }

    }

    public Boolean checkPwd(int teacherId,String pwd){
        LambdaQueryWrapper<Teacher> wrapper = new LambdaQueryWrapper();
        wrapper.eq(Teacher::getTeaID,teacherId)
                .eq(Teacher::getTeaPwd,pwd);
        int count = mapper.selectCount(wrapper);
        if(count==1){
            return true;
        }else{
            return false;
        }

    }
}
